﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy4_6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // 窗体加载事件：初始化滚动条和图像
        private void Form1_Load(object sender, EventArgs e)
        {
            InitHScrollBar();
            InitPicture();
        }

        // 初始化滚动条
        private void InitHScrollBar()
        {
            hScrollBar1.Minimum = 0;
            hScrollBar1.Maximum = 119;  // 需预留20宽度，故最大值设为119
            hScrollBar1.SmallChange = 5;
            hScrollBar1.LargeChange = 20;
            hScrollBar1.Value = 100;    // 初始缩放比例为100%
            hScrollBar1.Dock = DockStyle.Bottom;
        }

        // 初始化图像
        private void InitPicture()
        {
            pictureBox1.Image = Image.FromFile("flower.jpg"); // 加载图像
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;  // 保持图像比例
            pictureBox1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; // 随窗体大小自适应
        }

        // 图像缩放方法
        private void ZoomPic(int ratio)
        {
            // 计算缩放后的宽度和高度（基于窗体可用区域和缩放比例）
            int newWidth = (int)((this.ClientSize.Width - hScrollBar1.Height) * ratio / 100.0);
            int newHeight = (int)((this.ClientSize.Height - hScrollBar1.Height) * ratio / 100.0);

            // 调整PictureBox大小并居中显示
            pictureBox1.Width = newWidth;
            pictureBox1.Height = newHeight;
            pictureBox1.Left = (this.ClientSize.Width - pictureBox1.Width) / 2;
            pictureBox1.Top = (this.ClientSize.Height - hScrollBar1.Height - pictureBox1.Height) / 2;

            // 窗体标题显示缩放比例
            this.Text = $"图像浏览器 {ratio}%";
        }

        // 滚动条滚动事件：触发图像缩放
        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            ZoomPic(hScrollBar1.Value);
        }
    }
}
